#include <job/lge_jobsubsystem.h>

bool JobSubsystem::init(const Configuration & config)
{
	mConfiguration = config;

	mExitThreads = false;

	if (!mConfiguration.mThreadCount)
		mConfiguration.mThreadCount = processorCount();

	if (!mConfiguration.mMaxJobsPerQueue)
		return false;

	mJobQueues.reserve(mConfiguration.mThreadCount);

	// Main thread job queue
	mJobQueues.push_back(new JobQueue(mConfiguration.mMaxJobsPerQueue));

	for (SizeT i=1; i<mConfiguration.mThreadCount; i++)
	{
		mJobQueues.push_back(new JobQueue(mConfiguration.mMaxJobsPerQueue));	

		ThreadFunction func = createThreadFunction(
			&JobSubsystem::run, 
			this,
			i);
		
		mThreadPool.addThread(func);
	}

	return true;
}

bool JobSubsystem::release()
{
	mExitThreads = true;
	mThreadPool.clear();

	for (SizeT i=0; i<mConfiguration.mThreadCount; i++)
		delete mJobQueues[i];

	mJobQueues.clear();

	return true;
}
